import {defineStore} from "pinia";
// @ts-ignore
import {ajax, requestCache} from "@/js/api_tool.js";
// @ts-ignore
import {cacheGet, cacheSet} from "@/store/pluginCache.js";
import {S94Model} from "@/js/S94Model.ts";

interface TypeConfigItem {
    [key:string]: string | TypeConfigItem;
}

interface TypeConfig {
    order?: TypeConfigItem;
    page?: TypeConfigItem;
    pay?: TypeConfigItem;
    storage?: TypeConfigItem;
    system?: TypeConfigItem;
    user?: TypeConfigItem;
}

interface TypeEnumTitleItem {
    model_name: string;
    group: string;
    title: string;
    head_img: string | null;
    value_limit_json: string;
    sort: number;
    is_system: number;
    id: number;
    create_time: string;
    delete_time: number;
    tenant_id: number;
}

interface TypeEnumTitleMap {
    [model_name: string] : {
        [group: string] : TypeEnumTitleItem[];
    };
}

type TypeModelItem = typeof S94Model;

interface TypeStatusAliasMap {
    [table: string] : {
        [group: string] : {
            [key: string]: string;
        }
    }
}

interface TypeTenant {
    tenant_package_id: number;
    name: string;
    title: string;
    head_img: string;
    host: string;
    phone: string | null;
    link_truename: string | null;
    link_info: string | null;
    authorisation: string | null;
    company_full_name: string | null;
    kickback_type_ids: string | null;
    expiration_time: string | null;
    allow_expiration: 0 | 1;
    template: string;
    price_extend: string;
    id: number;
    create_time: string;
    delete_time: number;
    tenant_id: number;
    money: string;
    package_title: string;
    encode_id: string;
    max_expiration_time: string | null;
    expiration_status: 0 | 1 | 2;
}

interface TypePageItem {
    title: string;
    url: string | null;
    type: string;
    model_name: string | null;
    config_json: string;
    id: number;
    create_time: string;
    delete_time: number;
    tenant_id: number;
}



export const use_config_map = defineStore('config_map', {
    state: (): {
        config: TypeConfig;
        enum_title_map: TypeEnumTitleMap;
        model_map: {
            [name: string]: S94Model;
        };
        page_map: {
            [id: number]: TypePageItem;
        }
        status_alias_map: TypeStatusAliasMap;
        this_tenant: TypeTenant;
    } => {
        return {
            config: {
                order: {},
                page: {},
                pay: {},
                storage: {},
                system: {},
                user: {},
            },
            enum_title_map: {},
            model_map: {},
            page_map: {},
            status_alias_map: {},
            this_tenant: {
                tenant_package_id: 0,
                name: "",
                title: "",
                head_img: "",
                host: "",
                phone: null,
                link_truename: null,
                link_info: null,
                authorisation: null,
                company_full_name: null,
                kickback_type_ids: null,
                expiration_time: null,
                allow_expiration: 0,
                template: "",
                price_extend: "{}",
                id: 0,
                create_time: "",
                delete_time: 0,
                tenant_id: 0,
                money: "0",
                package_title: "",
                encode_id: "",
                max_expiration_time: null,
                expiration_status: 1
            }
        }
    },
    actions: {
        /**通过请求接口，获取数据后合并，来刷新数据
         */
        async refresh(){
            try {
                let {data} = await requestCache({url: '/home/api/config_map', showMessage: ajax.SHOW_MESSAGE_NONE});
                let old = cacheGet(this.$id);
                const old_json = JSON.stringify(old);
                const data_json = JSON.stringify(data);
                if (old_json !== data_json){
                    console.log(`refresh ${this.$id} ok`)
                    this.$patch(data);
                    cacheSet(this.$id, data);
                }
            } catch (e:any) {
                console.log(`refresh fail: ${e.message || e}`)
            }
        },
    }
})

export const use_config = ()=>{
    return use_config_map().config;
}
export const use_enum_title_map = ()=>{
    return use_config_map().enum_title_map;
}
export const use_model_map = ()=>{
    return use_config_map().model_map;
}
export const use_page_map = ()=>{
    return use_config_map().page_map;
}
export const use_status_alias_map = ()=>{
    return use_config_map().status_alias_map;
}
export const use_this_tenant = ()=>{
    return use_config_map().this_tenant;
}